﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellGene_h
#define CellGene_h

#include "CellPrerequisites.h"
#include "CellPropertyReference.h"
#include "CellObservable.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

// 前置声明
class AbstractGene;
class GeneContainer;

typedef std::list<Gene*> GeneList;

/** 基因接口类。
Cell Cloud 管理的基本云数据单元。
@author
	Jiangwei Xu
*/
class _CellExport Gene : public Observable
{
public:
	/** 默认构造函数。
	*/
	Gene(const String& code);

	/** 析构函数。
	*/
	virtual ~Gene(void);

public:
	/// 
	enum GeneMask
	{
		GM_TENEMENT = 0,
		GM_PAT_FILE,
		GM_PROPERTY,
		GM_EOF
	};

	/**
	*/
	const String& getCode(void) const;

public:
	/**
	*/
	virtual uint getMask(void) const = 0;

	/**
	*/
	virtual Cellet* getOwner(void) const = 0;

	/**
	*/
	virtual void setOwner(Cellet* owner) = 0;

	/**
	*/
	virtual void setParent(Gene* container) = 0;

	/**
	*/
	virtual Gene* getParent(void) = 0;

protected:
	/**
	*/
	virtual void setManaged(bool value) = 0;

	/**
	*/
	virtual bool getManaged(void) const = 0;

private:
	String mCode;

	friend class Cellet;
	friend class GeneContainer;
};



/**
*/
class _CellExport AbstractGene : public Gene
{
public:
	/**
	*/
	AbstractGene(const String& code);

	/**
	*/
	virtual ~AbstractGene(void);

public:
	/**
	*/
	virtual Cellet* getOwner(void) const;

	/**
	*/
	virtual void setOwner(Cellet* owner);

	/**
	*/
	virtual void setParent(Gene* gene);

	/**
	*/
	virtual Gene* getParent(void);

private:
	/**
	*/
	virtual void setManaged(bool value);

	/**
	*/
	virtual bool getManaged(void) const;

private:
	Cellet* mCellet;
	Gene* mParent;
	bool mManaged;
};



/** 基因容器事件。
*/
struct GeneContainerEvent
{
	static const String INSERT;
	static const String ERASE;
};

/** 基因容器。
*/
class _CellExport GeneContainer : public AbstractGene
{
public:
	/**
	*/
	GeneContainer(const String& code);

	/**
	*/
	virtual ~GeneContainer(void);

public:
	/**
	*/
	bool insert(Gene* gene, bool managed = true);

	/**
	*/
	void erase(Gene* gene);

	/**
	*/
	Gene* find(const String& code);

private:
	/**
	*/
	void destroyGene(Gene* gene);

private:
	typedef stx::btree_map<String, Gene*> Genes;
	Genes mGenes;

	CELL_MUTEX(mMutex);
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellGene_h
